from django.views import View
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from app01 import models
from app01.forms.deploytask import DeployTaskModelForm

class DeployTaskListView(View):
    '''
    项目发布的列表视图
    '''
    def get(self,request,pid):
        project_obj=models.Project.objects.filter(pk=pid).first()
        if not project_obj:
            return HttpResponse('没有该项目,获取数据失败')
        deploytask_queryset=models.DeployTask.objects.filter(project=project_obj).all()
        return render(request,'deploytask_list.html',{'task_list':deploytask_queryset,'project_obj':project_obj})

class DelpoyTaskAddView(View):
    '''
    项目发布的添加视图
    '''
    def get(self,request,pid):

        project_obj = models.Project.objects.filter(pk=pid).first()
        if not project_obj:
            return HttpResponse('没有该项目,获取数据失败')
        form=DeployTaskModelForm(project_obj)
        return render(request,'deploytask_add.html',{'form':form,'project_obj':project_obj})

    def post(self,request,pid):

        project_obj = models.Project.objects.filter(pk=pid).first()
        if not project_obj:
            return HttpResponse('没有该项目,获取数据失败')

        form=DeployTaskModelForm(project_obj,data=request.POST)
        if form.is_valid():
            form.save()
            url=reverse('deploytask_list',kwargs={'pid':project_obj.pk})
            return redirect(url)
        else:
            return render(request,'deploytask_add.html',{'form':form,'project_obj':project_obj})

class DelpoyTaskEditView(View):

    def get(self,request,pid,pk):
        return HttpResponse('编辑')

class DelpoyTaskDeleteView(View):

    def get(self,request,pid,pk):
        return HttpResponse('删除')



class DeployTaskDeployView(View):

    def get(self,request,pk):
        task_obj=models.DeployTask.objects.filter(pk=pk).first()
        if not task_obj:
            return HttpResponse('无效id')
        project_obj=task_obj.project
        return render(request,'deploytaskdeploy.html',{'task_obj':task_obj,'project_obj':project_obj})